import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;

import com.amazonaws.services.s3.model.S3Object;

public class PartB {

		private static String trueWordsFile = "wordsim-pos.txt";
		private static String falseWordsFile = "wordsim-neg.txt";
		
		private static ArrayList<PairDataPartB> data;
		private static HashSet<String> inPosTestSet;
		private static HashSet<String> inNegTestSet;
		private static double th;

		
		public static void main(String[] args)  {

			initMembers();
			
			if(args.length > 0) {
				th = Double.parseDouble(args[0]);
			}
			
			try {
				readTestSetToHash();
			} catch (IOException e) {
				System.out.println("Problem when trying to read test set");
				return;
			}
			
			try {
				readLastFullDecadeData();
			} catch (IOException e) {
				System.out.println("Problem when trying to read last full decade data");
				return;
			}
			
			double f = calcF();
			System.out.println(f);
			
		}

		private static void initMembers() {
			inPosTestSet = new HashSet<String>();
			inNegTestSet = new HashSet<String>();
			data = new ArrayList<PairDataPartB>();
			th = 3;
		}

		private static void readTestSetToHash() throws IOException {
			insertWord(true);
			insertWord(false);
		}

		private static void insertWord(boolean isTrueWords) throws IOException {
			FileReader file; 
			HashSet<String> hash;
			if(isTrueWords)
			{
				file = new FileReader(trueWordsFile);
				hash = inPosTestSet;
			}
			else
			{
				file = new FileReader(falseWordsFile);
				hash = inNegTestSet;
			}
			
			BufferedReader reader = new BufferedReader(file);
			
			String line = reader.readLine();
			
			while(line != null)
			{
				String[] split = line.split("\t");
				String words;
				if(split[0].compareTo(split[1]) <= 0) {
					words = split[0].trim() + "\t" + split [1].trim();
				}
				else {
					words = split[1].trim() + "\t" + split [0].trim();
				}
				hash.add(words);
				line = reader.readLine();
			}
		}

		private static void readLastFullDecadeData() throws IOException {
			System.out.println("before requesting file from s3");
			//S3Object fileDataObj = Utils.getObjectFromS3ByKey(PartA.lastDecadeFileName, JobBuilder.credentials.getCredentials());
			//BufferedReader br = new BufferedReader(new InputStreamReader(fileDataObj.getObjectContent()));
			FileReader file = new FileReader(PartA.lastDecadeFileName);
			BufferedReader br = new BufferedReader(file);
			System.out.println("after requesting file from s3");
			 
			
			String line = br.readLine();
			int total = 1;
			int pos = 0;
			int neg = 0;
			while(line != null)
			{
				String[] lineSplitted = line.split("\t");
				String firstWord = lineSplitted[0];
				String secondWord = lineSplitted[1];
				double pmi = Double.parseDouble(lineSplitted[2]);
				PairDataPartB pair = null;
				
				if(inPosTestSet.contains(firstWord + "\t" + secondWord) == true) { 
					pair = new PairDataPartB(firstWord, secondWord, pmi, true);
					System.out.println("in pos 1=" + firstWord + " 2=" + secondWord + " pmi=" + pmi);
					pos++;
				}
				else if (inNegTestSet.contains(firstWord + "\t" + secondWord) == true) {
					pair = new PairDataPartB(firstWord, secondWord, pmi, false);
					System.out.println("in neg 1=" + firstWord + " 2=" + secondWord + " pmi=" + pmi);
					neg++;
				}
				
				if(pair != null)
				{
					data.add(pair);
				}
				
				line = br.readLine();
				total++;
			}
			System.out.println("total lines=" + total + " pos=" + pos + " neg=" + neg);
		}
	

		private static double calcF() {
			
			double tp = 0;
			double tn = 0;
			double fp = 0;
			double fn = 0;
			double precision = 0;
			double recall = 0;
			double f;
			
			for (PairDataPartB pair : data) {
				if(pair.isInTrueWords() == true)
				{
					if(pair.getPmi() >= th)
					{
						tp++;
					}
					else
					{
						fn++;
					}
				}
				else
				{
					if(pair.getPmi() >= th)
					{
						fp++;
					}
					else
					{
						tn++;
					}
				}
			}
			
			if(tp + fp == 0)
				precision = 0;
			else
				precision = tp/(tp+fp);
			
			if(tp + fn == 0)
				recall = 0;
			else
				recall = tp/(tp+fn);
			
			if(precision + recall == 0)
				f = 0;
			else
				f = 2*( (precision*recall) / (precision + recall) );
			
			return f;
		}			

}
